package com.framework.persist.dbmaper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.framework.util.StringUtils;

public class UnderLineDBToBeanParse implements IDBToBeanParse{

	public String getBeanName(String tableName) {
		Pattern pattern = Pattern.compile("_[a-z]{1}");
		Matcher matcher = pattern.matcher(tableName.toLowerCase());
		String result = tableName.toLowerCase();
		while(matcher.find()){
			result = result.replace(matcher.group(), matcher.group().substring(1).toUpperCase());
		}
		return StringUtils.firstUp(result);
	}

	public String getFieldName(String columnName) {
		Pattern pattern = Pattern.compile("_[a-z]{1}");
		Matcher matcher = pattern.matcher(columnName.toLowerCase());
		String result = columnName.toLowerCase();
		while(matcher.find()){
			result = result.replace(matcher.group(), matcher.group().substring(1).toUpperCase());
		}
		return StringUtils.firstLower(result);
	}

	public String getTableName(String beanName) {
		Pattern pattern = Pattern.compile("[A-Z]{1}");
		Matcher matcher = pattern.matcher(StringUtils.firstLower(beanName));
		String result = StringUtils.firstLower(beanName);
		while(matcher.find()){
			result = result.replace(matcher.group(), "_"+matcher.group().toLowerCase());
		}
		return result.toUpperCase();	
	}

	public String getColumnName(String fieldName) {
		Pattern pattern = Pattern.compile("[A-Z]{1}");
		Matcher matcher = pattern.matcher(fieldName);
		String result = fieldName;
		while(matcher.find()){
			result = result.replace(matcher.group(), "_"+matcher.group().toLowerCase());
		}
		return result.toUpperCase();		
	}

	public static void main(String[] args) {
		UnderLineDBToBeanParse parse = new UnderLineDBToBeanParse();
		System.out.println(parse.getBeanName("SYSTEM_USER_INFO"));
		System.out.println(parse.getFieldName("USER_NAME"));
		System.out.println(parse.getTableName("SystemUserInfo"));
		System.out.println(parse.getColumnName("userInfo"));
	}
}
